package com.simple.datax.service.impl;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleMessage;
import com.simple.datax.api.CommonSimpleException;
import com.simple.datax.api.Router;
import com.simple.datax.api.SimpleException;
import com.simple.datax.service.ResponseMessageService;
import com.simple.datax.utils.DateUtils;
import com.simple.datax.utils.MessageUtils;
import com.simple.datax.xml.ec.EC101ResponseDocument;
import com.simple.datax.xml.ec.MessageHeadDocument.MessageHead;
import com.simple.datax.xml.ec.ResponseDataDocument.ResponseData;

/**
 * @Title: ResponseMessageServiceImpl.java
 * @Package com.simple.datax.service.impl
 * @Description: 回复服务
 * @author Paladin Chen
 * @date 2012-12-27 下午4:00:51
 * @version V1.0
 */
// @ Service("responseMessage")
public class ResponseMessageServiceImpl implements ResponseMessageService {
	private static final Logger logger = LoggerFactory.getLogger(ResponseMessageServiceImpl.class);
	private String localEcode = "EC9999999999";
	private String localNode = "EC0001";

	private String msgidPrefix = "EC";

	// 生成应答报文，直接调用router
	private Router router;

	@Override
	public void response(SimpleMessage src, int responseCode, String responseMsg) throws SimpleException {
		this.response(src, responseCode, responseMsg, false);
	}

	@Override
	public void response(SimpleMessage src, int responseCode, String responseMsg, boolean isMust)
			throws SimpleException {
		if (src == null) {
			throw new CommonSimpleException("The SimpleMessage CAN NOT be null!!");
		} else {
			logger.debug("Begin to proc response...... responeCode(" + responseCode + ")" + src.toLog());

			boolean isToResponse = isMust;

			// 当isMust是false时候，src要求回复，则应答
			if (src.isNeedResponse()) {
				isToResponse = true;
			}

			// 对于FLOW过来的报文一律不回复
			if (src.isIntraMsg()) {
				logger.debug("This message is IntraMsg.");
				isToResponse = false;
			}

			// 对于 response的报文一律不回复
			if (src.isResponse()) {
				logger.debug("This message is Response msg.");
				isToResponse = false;
			}

			// 对于TCS报文，都不回执
			if ("TCS".equalsIgnoreCase(src.getMsgType())) {
				logger.debug("From TCS.");
				isToResponse = false;
			}

			if (StringUtils.isBlank(src.getSender()) || StringUtils.isBlank(src.getSenderNode())) {
				logger.debug("Cannot get response address:" + src.toLog());
				isToResponse = false;
			}

			if (isToResponse) {
				// TODO 完成消息应答报文, ！！！！！需要处理serviceCode
				// 对response类型的报文，不应答
				EC101ResponseDocument erd = EC101ResponseDocument.Factory.newInstance();

				String stime = DateUtils.getTimeStamp("yyyymmddhhMMss");
				erd.addNewEC101Response();
				erd.getEC101Response().addNewMessageBody();
				ResponseData rd = erd.getEC101Response().getMessageBody().addNewResponseData();
				MessageHead mh = erd.getEC101Response().addNewMessageHead();

				rd.setResponseCode(responseCode);
				rd.setResponseValue("[" + src.getMessageid() + "]" + responseMsg);
				rd.setResponseTime(stime);

				String sentMsgid = MessageUtils.BuildMessageId(msgidPrefix);
				mh.setMessageId(sentMsgid);
				mh.setSenderId(localEcode);
				mh.setSenderAddress(localEcode + "@" + localNode);
				mh.setReceiverAddress(src.getSender() + "@" + src.getSenderNode());
				mh.setReceiverId(src.getSender());
				mh.setMessageType("EC101Response");
				mh.setMessageTime(stime);

				SimpleMessage resSmsg = new SimpleMessage();

				resSmsg.setCommunParam(src.getCommunParam());
				resSmsg.setCommunType(src.getCommunType());
				resSmsg.setMessageid(src.getMessageid());
				resSmsg.setSentMessageid(sentMsgid);
				resSmsg.setEncoding(src.getEncoding());
				resSmsg.setSender(localEcode);
				resSmsg.setSenderNode(localNode);
				resSmsg.setIntraDoc(erd);
				resSmsg.setReceiver(src.getSender());
				resSmsg.setReceiverNode(src.getSenderNode());
				resSmsg.setRecvRawMsgid(src.getRecvRawMsgid()+".R");
				resSmsg.setMsgFormat(src.getMsgFormat());
				resSmsg.setMsgType(src.getMsgType());
				resSmsg.setMsgBusiType("EC101Response");
				resSmsg.setServiceCode(src.getServiceCode());
				resSmsg.setResponse(true);
				resSmsg.setProcStatus(false);
				resSmsg.setIntraResponse(true);

				router.process(resSmsg);
			}
		}

		// logger.debug(src + "  :" + responseCode + "  :" + responseMsg);
		logger.debug("Finish proc response...... Code(" + responseCode + ")" + src.toLog());
	}

	public String getLocalNode() {
		return localNode;
	}

	public void setLocalNode(String localNode) {
		this.localNode = localNode;
	}

	public Router getRouter() {
		return router;
	}

	public void setRouter(Router router) {
		this.router = router;
	}

	public String getLocalEcode() {
		return localEcode;
	}

	public void setLocalEcode(String localEcode) {
		this.localEcode = localEcode;
	}

	public String getMsgidPrefix() {
		return msgidPrefix;
	}

	public void setMsgidPrefix(String msgidPrefix) {
		this.msgidPrefix = msgidPrefix;
	}
}
